home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / HEAD.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  1KB  |  41 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NUL '\000'
  6. #define BEL '\007'
  7. #define LINE_LEN 132
  8.  
  9. void give_up(char *msg)
  10. {
  11.       putchar(BEL);
  12.       puts(msg);
  13.       exit(-1);
  14. }
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.       FILE *infile;
  19.       char line[LINE_LEN + 2];                  /* Allow for '\n' & NUL */
  20.       int i, N = 0;
  21.  
  22.       if (2 > argc)
  23.             give_up("Usage: HEAD file [number_of_lines]");
  24.       if (NULL == (infile = fopen(argv[1], "r")))
  25.             give_up("Unable to open input file");
  26.       if (2 < argc)
  27.             N = atoi(argv[2]);
  28.       if (!N) N = 4;
  29.       for (i = 0; i < N; ++i)
  30.       {
  31.             if (NULL == fgets(line, LINE_LEN + 1, infile))
  32.                   break;
  33.             line[LINE_LEN + 1] = NUL;           /* Allow too-long lines */
  34.             fputs(line, stdout);
  35.             if (!strrchr(line, '\n'))
  36.                   i -= 1;                       /* More to read         */
  37.       }
  38.       fclose(infile);
  39.       return EXIT_SUCCESS;
  40. }
  41.